import os
import pymongo

CONNECTION_STR = 'mongodb://192.168.0.196:27017/?readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false'
# CONNECTION_STR = 'mongodb://192.168.0.196:27017/'
DATA_BASE = 'movies_record'  # 数据库名
DB_COL = 'movies_record'  # 表名
ONE_PATH = [['\\\\lcnas\\xishu16t2020_1\\10-素人AV\\', '素人AV'],
            ['\\\\lcnas\\xishu16t2020_1\\11-FC2有码\\', 'FC2有码'],
            ['\\\\lcnas\\xishu16T20000\\12-遮挡机动车\\', '有码'],
            ['\\\\lcnas\\xishu16T_2022\\07-FC2\\', 'FC2'],
            ]
MULTIPLE_PATH = []


def read_one_path(path):
    # 连接数据库
    db_client = pymongo.MongoClient(CONNECTION_STR)
    db_database = db_client[DATA_BASE]
    db_col = db_database[DB_COL]

    files = os.listdir(path[0])
    for file in files:
        movie_num = file
        # 查询是否存在
        query = {'movie_num': movie_num}
        projection = {'_id': 1}
        ans = list(db_col.find(query, projection))
        # 没有查到，插入
        if len(ans) == 0:
            classification = path[1]
            movie_num = file
            tor = ''
            is_collect = 'N'  # 是否收藏
            print(classification, movie_num, tor, is_collect)
            my_dict = {'classification': classification, 'movie_num': movie_num, 'tor': tor, 'is_collect': is_collect}
            db_col.insert_one(my_dict)
        else:
            print('{}已存在'.format(movie_num))
    db_client.close()


if __name__ == '__main__':
    for path in ONE_PATH:
        read_one_path(path)
